//
//  XJ_SelectBandView.m
//  XunJieProject
//
//  Created by xue on 2018/6/11.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_SelectBandView.h"
#import "XJ_SelectBandCell.h"
#import "XJ_SelectBandCell1.h"
#import "XJ_BankListModel.h"
///alertView  宽
#define AlertW SCREEN_WIDTH-40
@interface XJ_SelectBandView()<UITableViewDelegate,UITableViewDataSource>
// 背景视图
@property (nonatomic, strong) UIView *backgroundView;
//弹窗
@property (nonatomic,strong) UIView *alertView;
//确认按钮
@property (nonatomic,retain) UIButton *sureBtn;
//取消按钮
@property (nonatomic,retain) UIButton *cancleBtn;

@property (nonatomic,strong) UITableView *tableView;
//type 0实名认证和添加信用卡XJ_SelectBandCell1 只有文字 前面选择图片
//1 取现 信用卡 XJ_SelectBandCell 银行名称 卡号后四位 logo
//2 取现 储蓄卡 XJ_SelectBandCell 银行名称 卡号后四位 logo

@property (nonatomic,strong) NSString *type;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger index;
@end
@implementation XJ_SelectBandView

- (instancetype)initWithTitle:(NSString*)title cellType:(NSString*)type array:(NSMutableArray*)dataArr{
    
    if (self == [super init]) {
        
        self.type =type;
        self.dataArray = [NSMutableArray arrayWithCapacity:0];
        
        if ([self.type isEqualToString:@"0"]) {//添加信用卡的 发卡银行列表
            [dataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                XJ_BankListModel *model = [obj copy];
                [self.dataArray addObject:model];
                
            }];
        }else{
            
            [self.dataArray addObjectsFromArray:dataArr];
        }
        
        self.frame = [UIScreen mainScreen].bounds;
        [self addSubview:self.backgroundView];
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 5.0;
        self.alertView.clipsToBounds =YES;
        self.alertView.frame = CGRectMake(0, 150, AlertW, SCREEN_HEIGHT-150-80);
        
        
        UILabel *titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, AlertW, 50)];
        titleLabel.backgroundColor =ClearColor;
        titleLabel.text =title;
        titleLabel.textAlignment =NSTextAlignmentCenter;
        titleLabel.textColor =RGBCOLOR(152, 153, 154);
        if ([title isEqualToString:@"信用卡"]||[title isEqualToString:@"储蓄卡"]) {
            titleLabel.textColor =[UIColor blackColor];
        }
        titleLabel.font =[UIFont systemFontOfSize:17.0];
        [self.alertView addSubview:titleLabel];
        
        [self.alertView addSubview:self.tableView];
        
        if ([self.type isEqualToString:@"0"]) {//添加信用卡的 发卡银行列表
            
            CGFloat wideBtn =(SCREEN_WIDTH-40-60)/2;
            self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            
            self.cancleBtn.frame = CGRectMake(20, CGRectGetMaxY(self.tableView.frame)+14, wideBtn, 40);
            _cancleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            
            [self.cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
            [self.cancleBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            self.cancleBtn.tag = 1;
            [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            self.cancleBtn.layer.borderColor =[UIColor lightGrayColor].CGColor;
            self.cancleBtn.layer.borderWidth =0.8;
            self.cancleBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
            [self.alertView addSubview:self.cancleBtn];
            
            
            
            
            
            self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            self.sureBtn.frame = CGRectMake(20+wideBtn+20, CGRectGetMaxY(self.tableView.frame)+14, wideBtn, 40);
            _sureBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
            [self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.sureBtn.tag = 2;
            self.sureBtn.backgroundColor =Color_ZhuSeDiao;
            self.sureBtn.titleLabel.font =[UIFont systemFontOfSize:15.0];
            [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            [self.alertView addSubview:self.sureBtn];
            
            
            self.alertView.frame =CGRectMake(0, 150, AlertW, CGRectGetMaxY(_sureBtn.frame)+14);
        }else{
            
            UIButton *backBtn =[UIButton buttonWithType:UIButtonTypeCustom];
            backBtn.backgroundColor =ClearColor;
            backBtn.frame =CGRectMake(AlertW-40-2, 6, 44, 44);
            [backBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
            [self.alertView addSubview:backBtn];
            [backBtn addTarget:self action:@selector(Click_hidden) forControlEvents:UIControlEventTouchUpInside];
            
            UIView *linView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame), AlertW, 1)];
            linView.backgroundColor =Color_Line;
            [self.alertView addSubview:linView];
            
            self.alertView.frame =CGRectMake(0, 150, AlertW, CGRectGetMaxY(_tableView.frame)+14);
        }
        
       
        [self addSubview:self.alertView];
        
    }
    return self;
}
#pragma mark - 弹出 -
- (void)showXLAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
    //    [self performSelector:@selector(Click_hidden) withObject:nil afterDelay:1.8];
}
-(void)Click_hidden{
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(UITableView*)tableView{
    
    if (!_tableView) {
        _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 50, AlertW, 300) style:UITableViewStylePlain];
        _tableView.backgroundColor =ClearColor;
        _tableView.delegate =self;
        _tableView.dataSource =self;
        _tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}
#pragma mark - 背景遮罩图层
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _backgroundView.userInteractionEnabled = YES;
        if ([self.type isEqualToString:@"1"]||[self.type isEqualToString:@"2"]) {
            UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
            [_backgroundView addGestureRecognizer:myTap];

            
        }
    }
    return _backgroundView;
}
#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
    }];
}
#pragma mark - 回调 -设置只有2  -- > 确定才回调
- (void)buttonEvent:(UIButton *)sender
{
    if (sender.tag==1) {
        
        
        
    }else if (sender.tag == 2) {
        XJ_BankListModel * bankModel = [self.dataArray objectAtIndex:self.index];
        if (self.block) {
            
            self.block(bankModel,self.dataArray);
        }
    }
    [self removeFromSuperview];
}

#pragma mark--UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * identifier = @"cell";
    if ([self.type isEqualToString:@"1"]||[self.type isEqualToString:@"2"]) {
        XJ_SelectBandCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            
            cell =[[[NSBundle mainBundle] loadNibNamed:@"XJ_SelectBandCell" owner:nil options:nil]firstObject];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.backgroundColor =[UIColor clearColor];
        }
        
        if ([self.type isEqualToString:@"1"]) {//信用卡
            XJ_CreditCardModel * CardModel = [self.dataArray objectAtIndex:indexPath.row];
            [cell.logoImage sd_setImageWithURL:[NSURL URLWithString:CardModel.bankLogo] placeholderImage:[UIImage imageNamed:@"img_no_image"]];
            cell.bandNameLabel.text =CardModel.bank;
            cell.cardIdLabel.text =[NSString stringWithFormat:@"(%@)",CardModel.cardNoTail];
        }else{
            XJ_DepositCardModel * DepositCardModel = [self.dataArray objectAtIndex:indexPath.row];
            [cell.logoImage sd_setImageWithURL:[NSURL URLWithString:DepositCardModel.bankLogo] placeholderImage:[UIImage imageNamed:@"img_no_image"]];
            cell.bandNameLabel.text =DepositCardModel.bank;
            cell.cardIdLabel.text =[NSString stringWithFormat:@"(%@)",DepositCardModel.cardNoTail];
        }
        

        
         return cell;
    }else{
        
        XJ_BankListModel * bankModel = [self.dataArray objectAtIndex:indexPath.row];
        
        XJ_SelectBandCell1 * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) {
            
            cell =[[[NSBundle mainBundle] loadNibNamed:@"XJ_SelectBandCell1" owner:nil options:nil]firstObject];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.backgroundColor =[UIColor clearColor];
            
        }
        cell.titleLabel.text = bankModel.bankName;
        if (bankModel.isSelect) {
            [cell.selectBtn setImage:[UIImage imageNamed:@"img_agree_c"] forState:UIControlStateNormal];
        }else{
            [cell.selectBtn setImage:[UIImage imageNamed:@"img_agree_n"] forState:UIControlStateNormal];

        }
         return cell;
    }
   
   
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.type isEqualToString:@"1"]||[self.type isEqualToString:@"2"]) {
        return 70;
    }
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
     if ([self.type isEqualToString:@"1"]) {
         
         XJ_CreditCardModel * CardModel = [self.dataArray objectAtIndex:indexPath.row];

         
         if (self.block) {
             self.block(CardModel,nil);
         }
         [self Click_hidden];
         
     }else if ([self.type isEqualToString:@"2"]) {
         
         XJ_DepositCardModel * CardModel = [self.dataArray objectAtIndex:indexPath.row];
         
         
         if (self.block) {
             self.block(CardModel,nil);
         }
         [self Click_hidden];
     }
     else{

//         XJ_BankListModel * bankModel = [self.dataArray objectAtIndex:indexPath.row];
//
//         if (self.block) {
//
//             self.block(bankModel.bankName,bankModel.bankId);
//         }
         
         self.index =indexPath.row;
         for (XJ_BankListModel * bankModel in _dataArray) {
             bankModel.isSelect =NO;
         }
         XJ_BankListModel * select_bankModel =_dataArray[indexPath.row];
         select_bankModel.isSelect =YES;
         
         [self.tableView reloadData];
     }
   
    
}


@end
